# coding: utf-8
import os, sys
home = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.append('/home/lanwenhong/tianya/trunk/client/python')
from memlinkclient import *
import base_op
import urllib
import urllib2
import json

class IndexAddTest:
    def __init__(self, memlink_path, memlink_addr, wport, rport, dserver, dport, htdocs_path):
        self.bop = base_op.BaseOp(memlink_path, memlink_addr, wport, rport, dserver, dport, htdocs_path)

    def test(self, tablename, itemcount):
        self.bop.create_table(tablename, itemcount)
        ret , jstr = self.bop.create_test_data(tablename, itemcount)
        if ret == -1:
            return ret

        mdic = {}
        keylist = []
        
        for i in xrange(1, itemcount):
            indexkey = 'item%d' % i
            ret = self.bop.create_index(tablename, indexkey, 'index_add')
            if (ret == -1)
                return -1
        
        jdic = json.loads(jstr)
        linelist = jdic['array']
        #print linelist
        for i in xrange(1, itemcount):
            item = 'item%d' % i
            for line in linelist:
                value = line[item]
                mkey = '%s%s_%s' % ('index_add', item, value) 
                #print '========================================' , mkey
                if mkey not in mdic:
                    mdic[mkey] = []
                    mdic[mkey].append(line['item0'])
                    keylist.append(mkey)

                else:
                    mdic[mkey].append(line['item0'])
        
        testdic = {}
        print self.bop.tinit.memlink_addr
        print self.bop.tinit.rport
        print self.bop.tinit.wport
        m = MemLinkClient(self.bop.tinit.memlink_addr, self.bop.tinit.rport, self.bop.tinit.wport, 30) 
        
        print keylist
        for mkey in keylist:
            print mkey
            ret, recs = m.range(str(mkey), MEMLINK_VALUE_ALL, 0, 1000, '')
            if ret != MEMLINK_OK:
                print 'range error:', ret
                return -1
            mlist = []
            items = recs.root
            while items:
                mlist.append(items.value)
                items = items.next
            testdic[mkey] = mlist 
            
        m.destroy() 
        print 'mdic', mdic
        print 'testdic', testdic

        for key in keylist:
            cmplist = mdic[key]
            rowkeylist = testdic[key]

            print '-----------cmplist:' , cmplist
            print '-----------rowkeylist:', rowkeylist

            if len(cmplist) != len(rowkeylist):
                print 'len cmp error'
                return -1 

            for i in cmplist:
                if i not in rowkeylist:
                    print '%s not in rowkeylist' % i
                    return -1

        print 'test succ'

        self.bop.test_clean(tablename, self.bop.tinit.htdocs_path, self.bop.tinit.memlink_path)
        return 0

def index_add_test():
    iatest = IndexAddTest('/home/lanwenhong/tianya/trunk', '127.0.0.1', 16001, 16002, '127.0.0.1', 8800, '/home/lanwenhong/htdocs') 
    iatest.test('index_add_test_table', 4)

if __name__ == '__main__':
    index_add_test()

